package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;



/**
 * @ClassName StsController
 * @Description TODO
 * @Author lvzhenzhen
 * @Date 2021/12/7 7:34
 */
@Controller
@RequestMapping("/sts")
public class StsController {
    @RequestMapping("/getToken.do")
    @ResponseBody
    public String getStsToken() {
        //构建一个阿里云客户端，用于发起请求。
        //构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI5tR4SdwCgQcvGKc3iPQ9",
                "Pl0jofF5b1bLMSW9SGwm4Fgnt8ZC5d");
        profile.getCredential();
        // 临时凭证的有效期，单位是s，最小为900，最大为3600
     //   long sessionTime = 900;
        IAcsClient client = new DefaultAcsClient(profile);
        System.out.println(JSON.toJSONString(client));

        //构造请求，设置参数。关于参数含义和设置方法，请参见《API参考》。
        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setRegionId("cn-hangzhou");
        request.setRoleArn("acs:ram::1242922078485173:role/admin");
        request.setRoleSessionName("external-username");
       //request.setDurationSeconds(sessionTime);

        //发起请求，并得到响应。
        try {
            AssumeRoleResponse response = client.getAcsResponse(request);
            return JSON.toJSONString(response);

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return "获取不到sts token";
    }

//    public static void main(String[] args) {
//        //构建一个阿里云客户端，用于发起请求。
//        //构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
//        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing",
//                "LTAI5t6yGYUrdHqjvWCQPgDL",
//                "rDOjk8yAvuqyjk2RR5HLAeyN5DOCUd");
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        //构造请求，设置参数。关于参数含义和设置方法，请参见《API参考》。
//        AssumeRoleRequest request = new AssumeRoleRequest();
//        request.setRegionId("cn-hangzhou");
//        request.setRoleArn("<RoleArn>");
//        request.setRoleSessionName("<RoleSessionName>");
//
//        //发起请求，并得到响应。
//        try {
//            AssumeRoleResponse response = client.getAcsResponse(request);
//            System.out.println(new Gson().toJson(response));
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            System.out.println("ErrCode:" + e.getErrCode());
//            System.out.println("ErrMsg:" + e.getErrMsg());
//            System.out.println("RequestId:" + e.getRequestId());
//        }
//
//    }

    public static void main(String[] args) {
        long now=1638907942947l;   //获取出来的是当前时间的毫秒值

        //把毫秒值转换成时间格式
        Date d=new Date();
        d.setTime(now);

        System.out.println(d.toString());

    }


}
